package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.admin.UserService;
import com.cskaoyan.util.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lc
 * @date 2022/06/25 11:05
 */
@RestController
@RequestMapping("admin/user")
public class AdminUserController {

    @Autowired
    UserService userService;

    /*@RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit, String sort,String WXUserIndexVo) {

    }*/

    /**
     * 通过username/mobile/userId进行查找用户,都为空时显示所有用户
     * userId转为int,错误则返回{"errno":402,"errmsg":"参数值不对"}
     * <p>
     * 注: 考虑多了,不影响 未删除
     * 请求url中有id 但实际请求userid始终为空 含有id时查找使用detail方法
     *
     * @param /username/mobile/userId
     * @return 用户信息
     * @author LC_R-SH
     * @since 2022/06/25 20:06
     */
    //访问url需要对应的权限。
    //@RequiresPermissions("aaa")
    @RequestMapping("list")
    public BaseRespVo list(String username, String mobile, String userId, BasePageInfo info) {
        Integer id = 0;
        try {
            id = Integer.parseInt(userId.trim());
        } catch (Exception e) {
            System.out.println("id为空");
        }
        CommonData<MarketUser> data = userService.query(username, mobile, id, info);
        return BaseRespVo.ok(data);
    }

    /**
     * 通过id查找用户信息
     * 判断参数是否合法 不合法返回{"errno":402,"errmsg":"参数值不对"}
     *
     * @param id
     * @return 用户信息
     * @author LC_R-SH
     * @since 2022/06/25 21:35
     */
    @RequestMapping("detail")
    public BaseRespVo detail(String id) {
        Integer userId = 0;
        if (id != null && id != "") {
            try {
                userId = Integer.parseInt(id.trim());
            } catch (Exception e) {
                return BaseRespVo.invalidParameter("无效参数");
            }
        }
        MarketUser user = userService.queryById(userId);
        return BaseRespVo.ok(user);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody@Validated MarketUser user, BindingResult bindingResult) throws MarketValidationException {
        ValidationUtils.valid(bindingResult);
        int affectRows = userService.update(user);
        if (affectRows != 0) {
            return BaseRespVo.ok(1);
        }
        return BaseRespVo.invalidData();
    }


}
